// pages/Earnings/IncomeSpending/IncomeSpending.js
//获取应用实例
const app = getApp();
const assetsAddress = app.globalData.global.assetsIp + '/' + app.globalData.global.assetsName;
import { getUserAccount } from '../../../api/user';
import { getAwardRecordSum , getTeamAmount } from '../../../api/earning';
const regeneratorRuntime = require('../../../common/runtime');
import { formatTime } from '../../../utils/util';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    shareTitle: app.globalData.global.shareTitle,
    assetsAddress: assetsAddress,
    navBgColor: 'none',
    totalSum: 0, // 累计收益
    todayHasTrans: 0, // 今日已到账
    todayNoTrans: 0, // 今日未到账
    balance: 0, // 当前余额
    buyAmount: 0, // 本人消费金额
    directBuyAmount: 0, // 直推消费金额
    teamBuyAmount: 0, // 团队消费金额
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this._getTotalSum();
    this._getTodayHasTrans();
    this._getTodayNoTrans();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this._getUserAccount();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this._getUserAccount();
    this._getTotalSum();
    this._getTodayHasTrans();
    this._getTodayNoTrans();
    setTimeout(()=>{
      wx.stopPullDownRefresh({
        success: (res) => {
          console.log(res,'刷新成功')
          wx.showToast({
            title: '刷新成功',
            icon: 'none',
          })
        },
      })
    },1000)
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    let userId = wx.getStorageSync('userId')
    return {
      title: this.data.shareTitle,
      path: '/pages/Welcome/Welcome?scene='+userId,
    }
  },
  // 页面跳转
  goNext(e){
    let type = e.target.dataset.type||e.currentTarget.dataset.type;
    let url = e.target.dataset.name||e.currentTarget.dataset.name;
    let item = e.target.dataset.item||e.currentTarget.dataset.item;
    // console.log(JSON.stringify(item))
    // return
    if(item){
      if(type == 1){
        wx.switchTab({
          url: `${url}`,
        })
      }else{
        wx.navigateTo({
          url: `${url}?item=${JSON.stringify(item)}`,
        })
      }
    }else{
      if(type == 1){
        wx.switchTab({
          url: `${url}`,
        })
      }else{
        wx.navigateTo({
          url: `${url}`,
        })
      }
      
    }
  },
  // 获取用户余额信息
  _getUserAccount(){
    let _this = this;
    getUserAccount()
    .then(res => {
      if(res.success){
        _this.setData({
          balance: res.data.balance,
        })
      }else{
        wx.showToast({
          title: res.errMessage,
          icon: 'none',
        })
      }
    })
  },
  // 查询累计收益
  async _getTotalSum(){
    let _this = this;
    try {
      var data = await _this._getAwardRecordSum({status: '1'});
      _this.setData({
        totalSum: data,
      })
    } catch (error) {
      console.log(error,'error')
      wx.showToast({
        title: error,
        icon: 'none',
      })
    }
  },
  // 查询今日已到账
  async _getTodayHasTrans(){
    let _this = this;
    var date = new Date();
    var gtDate = formatTime(date.getTime(),"Y-M-D")
    try {
      var data = await _this._getAwardRecordSum({gtDate: gtDate,status: '1'});
      _this.setData({
        todayHasTrans: data,
      })
    } catch (error) {
      wx.showToast({
        title: error,
        icon: 'none',
      })
    }
  },
  // 查询今日未到账
  async _getTodayNoTrans(){
    let _this = this;
    var date = new Date();
    var gtDate = formatTime(date.getTime(),"Y-M-D")
    try {
      var data = await _this._getAwardRecordSum({gtDate: gtDate,status: '0'});
      _this.setData({
        todayNoTrans: data,
      })
    } catch (error) {
      wx.showToast({
        title: error,
        icon: 'none',
      })
    }
  },
  // 查询收益累计接口
  _getAwardRecordSum({gtDate: gtDate,ltDate: ltDate,status: status}){
    return new Promise((resolve,reject) => {
      getAwardRecordSum({gtDate: gtDate,ltDate: ltDate,status: status})
      .then(res => {
        if(res.success){
          resolve(res.data)
        }else{
          reject(res.errMessage)
        }
      })
    }) 
  },
  // 获取消费金额
  _getTeamAmount(){
    let _this = this;
    getTeamAmount()
    .then(res => {
      if(res.success){
        _this.setData({
          buyAmount: res.data.buyAmount,
          directBuyAmount: res.data.directBuyAmount,
          teamBuyAmount: res.data.teamBuyAmount,
        })
      }else{
        wx.showToast({
          title: res.errMessage,
          icon: 'none',
        })
      }
    })
  },
  // 返回上一页
  back(){
    wx.navigateBack({
      delta: 1,
    })
  },
})